//
//  DRLoginViewController.m
//  DoctorRen
//
//  Created by Macx on 2018/11/18.
//

#import "DRLoginViewController.h"
#import "DRRegisterViewController.h"
#import "DRLoginDataModel.h"
#import <ShareSDK/ShareSDK.h>

@interface DRLoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *showPasswordBtn;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;


@end

@implementation DRLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.loginButton.layer.cornerRadius = 22;
    [self.showPasswordBtn setAdjustsImageWhenHighlighted:NO];
    [self.backBtn setHidden:[self isRoomVC]];
}

-(BOOL) isRoomVC{
    return [self.navigationController isEqual:[UIApplication sharedApplication].keyWindow.rootViewController];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    DRUserModel *model = [DRLoginDataModel getUserModel];
    if (model.phone) {
        self.phoneTF.text = model.phone;
//        self.passwordTF.text = model.realPw;
    }
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (IBAction)loginAction:(id)sender {
    if ([NSString isNullOrEmpty:self.phoneTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"电话不能为空"];
        return;
    }
    if ([NSString isNullOrEmpty:self.passwordTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"密码不能为空"];
        return;
    }
    [SVProgressHUD showWithStatus:@"正在登陆"];
    [DRLoginDataModel loginWithPhone:self.phoneTF.text Password:self.passwordTF.text Success:^(DRUserModel * _Nonnull user) {
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        [DRLoginDataModel setIsLogin:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccess object:nil];
        if ([self isRoomVC]) {
            UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            KEY_WINDOW.rootViewController = mainSB.instantiateInitialViewController;
            [KEY_WINDOW makeKeyAndVisible];
        }else{
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }
        
    } Fail:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
    
}

- (IBAction)registerAction:(id)sender {
    DRRegisterViewController *vc = [self getRegisterVC];
    vc.type = DRRegisterVC;
    vc.title = @"注册";
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)wxLoginAction:(id)sender {
    [ShareSDK getUserInfo:SSDKPlatformTypeWechat
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             [SVProgressHUD showWithStatus:@"正在登录"];
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
             [DRLoginDataModel thirdPartyLoginWithType:1 unionid:user.uid Success:^(DRUserModel *user) {
                 [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                 [self.phoneTF resignFirstResponder];
                 [self.passwordTF resignFirstResponder];
                 [DRLoginDataModel setIsLogin:YES];
                 [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccess object:nil];
                 if ([self isRoomVC]) {
                     UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                     KEY_WINDOW.rootViewController = mainSB.instantiateInitialViewController;
                     [KEY_WINDOW makeKeyAndVisible];
                 }else{
                     [self dismissViewControllerAnimated:YES completion:^{
                         
                     }];
                 }
                 
             } Fail:^(NSError *error) {
                 if ([error.localizedDescription isEqualToString: @"需要绑定手机号"] || [error.localizedDescription isEqualToString: @"未绑定手机号"] ) {
                     [SVProgressHUD showErrorWithStatus:@"需要先绑定手机号"];
                     [self gotoBindPhone:user.uid type:1];
                 }else{
                     [SVProgressHUD showErrorWithStatus:error.localizedDescription];
                 }
             }];
         }
         
         else
         {
             NSLog(@"%@",error);
             [SVProgressHUD showErrorWithStatus:error.localizedDescription];
         }
         
     }];
}

- (IBAction)qqLoainAction:(id)sender {
    //QQ的登录
    [ShareSDK getUserInfo:SSDKPlatformTypeQQ
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
             [SVProgressHUD showWithStatus:@"正在登录"];
             [DRLoginDataModel thirdPartyLoginWithType:2 unionid:user.uid Success:^(DRUserModel *user) {
                 [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                 sleep(1);
                 [DRLoginDataModel setIsLogin:YES];
                 [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccess object:nil];
                 if ([self isRoomVC]) {
                     UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                     KEY_WINDOW.rootViewController = mainSB.instantiateInitialViewController;
                     [KEY_WINDOW makeKeyAndVisible];
                 }else{
                     [self dismissViewControllerAnimated:YES completion:^{
                         
                     }];
                 }
             } Fail:^(NSError *error) {
                 if ([error.localizedDescription isEqualToString: @"需要绑定手机号"] || [error.localizedDescription isEqualToString: @"未绑定手机号"] ) {
                     [SVProgressHUD showErrorWithStatus:@"需要先绑定手机号"];
                     [self gotoBindPhone:user.uid type:1];
                 }else{
                     [SVProgressHUD showErrorWithStatus:error.localizedDescription];
                 }
             }];
         }
         
         else
         {
             NSLog(@"%@",error);
             [SVProgressHUD showErrorWithStatus:error.localizedDescription];
         }
         
     }];
}

- (IBAction)forgetPasswordAction:(id)sender {
    DRRegisterViewController *vc = [self getRegisterVC];
    vc.title = @"忘记密码";
    vc.type = DRForgetVC;
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void) gotoBindPhone:(NSString *) uid type:(NSInteger )type{
    DRRegisterViewController *vc = [self getRegisterVC];
    vc.title = @"绑定手机";
    vc.type = DRBindPhoneVC;
    vc.uid = uid;
    vc.bindType = type;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)showPasswordAction:(id)sender {
    [self.showPasswordBtn setSelected:!self.showPasswordBtn.isSelected];
    [self.passwordTF setSecureTextEntry:!self.showPasswordBtn.isSelected];
}

- (DRRegisterViewController *) getRegisterVC{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    DRRegisterViewController *vc = [sb instantiateViewControllerWithIdentifier:@"DRRegisterViewController"];
    return vc;
}
- (IBAction)clickBackButton:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}



@end
